package com.hq.fxh.No0_ChengQu.Login;

import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Config;
import com.hq.fxh.R;

import java.io.UnsupportedEncodingException;


/**
 * @author 大圣
 * @ClassName ResetPasswordActivity
 * @Description :密码重置
 * @Created 16/5/20
 * @Email qwqe601660957@163.com
 */
public class ResetPasswordActivity extends BaseActivity{
    private Button btnNext;
    private EditText etCurrentPassword;
    private EditText etNewPassword;
    private EditText etConfirmPassword;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset_password;
    }

    @Override
    public void initView() {
        btnNext = (Button) findViewById(R.id.btnNext);
        etCurrentPassword = (EditText) findViewById(R.id.etCurrentPassword);
        etNewPassword = (EditText) findViewById(R.id.etNewPassword);
        etConfirmPassword = (EditText) findViewById(R.id.etConfirmPassword);
    }

    @Override
    protected void initData() {
        setTitle("修改密码");
        registerListener();
        etCurrentPassword.setFilters(new InputFilter[]{
                new InputFilter.LengthFilter(20),
                new InputFilter(){

                    @Override
                    public CharSequence filter(CharSequence source, int start,
                                               int end, Spanned dest, int dstart, int dend) {

                        if(isCN(source.toString())){
                            if(dstart >= 0){
                                etCurrentPassword.setSelection(dstart);
                                return "";
                            }else{
                                if(source.length()+dest.length() > 0){
                                    return source.subSequence(start, start+0-dest.length());
                                }else{
                                    return source;
                                }
                            }
                        }else{
                            return source;
                        }
                    }

                }
        });

        etNewPassword.setFilters(new InputFilter[]{
                new InputFilter.LengthFilter(20),
                new InputFilter(){

                    @Override
                    public CharSequence filter(CharSequence source, int start,
                                               int end, Spanned dest, int dstart, int dend) {

                        if(isCN(source.toString())){
                            if(dstart >= 0){
                                etCurrentPassword.setSelection(dstart);
                                return "";
                            }else{
                                if(source.length()+dest.length() > 0){
                                    return source.subSequence(start, start+0-dest.length());
                                }else{
                                    return source;
                                }
                            }
                        }else{
                            return source;
                        }
                    }

                }
        });

        etConfirmPassword.setFilters(new InputFilter[]{
                new InputFilter.LengthFilter(20),
                new InputFilter(){

                    @Override
                    public CharSequence filter(CharSequence source, int start,
                                               int end, Spanned dest, int dstart, int dend) {

                        if(isCN(source.toString())){
                            if(dstart >= 0){
                                etCurrentPassword.setSelection(dstart);
                                return "";
                            }else{
                                if(source.length()+dest.length() > 0){
                                    return source.subSequence(start, start+0-dest.length());
                                }else{
                                    return source;
                                }
                            }
                        }else{
                            return source;
                        }
                    }

                }
        });
    }

    public boolean isCN(String str){
        try {
            byte [] bytes = str.getBytes("UTF-8");
            if(bytes.length == str.length()){
                return false;
            }else{
                return true;
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }








    public void registerListener() {
        etConfirmPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                confirmPwd = editable.toString();
                checkInput();
            }
        });
        etCurrentPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                currentPwd = editable.toString();
                checkInput();
            }
        });
        etNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                newPwd = editable.toString();
                checkInput();
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                resetPwd();
            }
        });
    }
    private String currentPwd;
    private String newPwd;
    private String confirmPwd;

    public void checkInput(){
        if(!TextUtils.isEmpty(currentPwd) && !TextUtils.isEmpty(newPwd) && !TextUtils.isEmpty(confirmPwd)){
            btnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    resetPwd();
                }
            });
            btnNext.setClickable(true);
            btnNext.setTextColor(getResources().getColor(R.color.blue));
        }else{
            btnNext.setOnClickListener(null);
            btnNext.setClickable(false);
            btnNext.setTextColor(getResources().getColor(R.color.gray_4));
        }
    }

    public void resetPwd(){
        if(TextUtils.isEmpty(etCurrentPassword.getText())){
            ToastUtils.show(this,"当前密码不能为空");
            return;
        }
        if(TextUtils.isEmpty(etNewPassword.getText())){
            ToastUtils.show(this,"新密码不能为空");
            return;
        }
        if(TextUtils.isEmpty(etConfirmPassword.getText())){
            ToastUtils.show(this,"确认密码不能为空");
            return;
        }

        if(TextUtils.equals(etNewPassword.getText(),etConfirmPassword.getText())){
            Intent intent = new Intent();
            intent.putExtra("password", etConfirmPassword.getText().toString());

            setResult(Config.ResultCode.PASSWORD, intent);
            finish();

        }else{
            ToastUtils.show(this,"两次输入的新密码不一致");
        }
    }
}
